/*
 * Copyright (c) 2005, 2014 vacoor
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package org.vacoor.nothing.misc.sap.jco.demo;

import com.sap.conn.jco.JCoException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.vacoor.nothing.misc.sap.jco.binding.Session;
import org.vacoor.nothing.misc.sap.jco.config.JcoConfig;
import org.vacoor.nothing.misc.sap.jco.demo.rfc.*;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * RFC Demo
 */
public class Demo {
    private static Logger logger = LoggerFactory.getLogger(Demo.class);

    public static void main(String[] args) throws JCoException, ParseException, IOException {
        final String destinationName = "as_pool";

        JcoConfig.addNativeLibDir("/home/vacoor/IdeaProjects/nothing/nothing-sap/src/local-lib");

        // 使用 classpath 配置
        JcoConfig jcoConfig = JcoConfig.configureUseClassPathFile();

        Session session = jcoConfig.openDestination(destinationName);
        RFCMapper mapper = session.getMapper(RFCMapper.class);

        SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd");
        Date date = formater.parse("2008-12-11");
        // date = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse("2008-12-11 22:22:22");    // 测试下是否是精确到天

        logger.info("开始获取 {} 物料变更信息...", date);
        List<MaterielChangeInfo> infos = mapper.getMaterielChangeInfos(date);
        for (MaterielChangeInfo mci : infos) {
            logger.info("获取物料变更信息: {}", mci);
        }

        logger.info("开始获取 {} 供应商变更信息...", date);
        for (DealerChangeInfo dci : mapper.getDealerChangeInfo(date)) {
            logger.info("获取供应商变更信息: {}", dci);
        }

        // -------------
        date = formater.parse("2013-04-11");
        logger.info("开始获取 {} 库存变更信息...", date);
        List<StockInfo> stockInfos = mapper.getStockInfo(date);
        for (StockInfo info : stockInfos) {
            logger.info("获取库存变更信息: {}", info);
        }

        logger.debug("开始获取 {} MBOM 变更信息...", date);
        for (BomChangeInfo bci : mapper.getBomChange(date)) {
            logger.info("获取 MBOM 变更信息: {}", bci);
        }

        date = formater.parse("2009-08-01");
        // date = formater.parse("2013-01-01");
        // date = formater.parse("2013-06-01");
        // date = formater.parse("2013-08-18");

        // date = formater.parse("2013-12-01");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        for (int i = 0; i < 10; i++) {
            date = calendar.getTime();
            logger.info("开始获取 {} 生产预测变更信息...", formater.format(date));
            for (ProductForecast pf : mapper.getProductForecast(date)) {
                logger.info("生产预测变更信息: {}", pf);
                if (date.getTime() > pf.getDatum().getTime()) {
                    logger.warn("生产预测时间 {} 小于获取时间 {}", formater.format(pf.getDatum()), formater.format(date));
                }
            }
            calendar.add(Calendar.DAY_OF_MONTH, 1);
        }
    }
}
